package com.asion.qkan.ui.view;

import android.app.Activity;
import android.content.Intent;
import android.content.res.TypedArray;
import android.os.Build;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.RequiresApi;
import android.support.design.widget.NavigationView;
import android.support.v4.app.FragmentTransaction;
import android.support.v4.view.GravityCompat;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBarDrawerToggle;
import android.support.v7.widget.Toolbar;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.TextView;

import com.asion.qkan.R;
import com.asion.qkan.api.AppConstant;
import com.asion.qkan.jiguangtuisong.ExampleApplication;
import com.asion.qkan.ui.activity.LoginActivity;
import com.asion.qkan.ui.activity.MusicActivity;
import com.asion.qkan.ui.fragment.CollectionFragment;
import com.asion.qkan.ui.fragment.FindFragment;
import com.asion.qkan.ui.fragment.HomeFragment;
import com.asion.qkan.ui.fragment.SheQuFragment;
import com.asion.qkan.utils.changeTheme.ThemeChangeUtil;

import butterknife.BindView;

public class MainActivity extends BaseActivity implements NavigationView.OnNavigationItemSelectedListener {

    @BindView(R.id.drawer_layout)
    DrawerLayout mDrawer;
    @BindView(R.id.toolbar)
    Toolbar mToolbar;
    @BindView(R.id.container)
    FrameLayout mContainer;
    @BindView(R.id.nav_view)
    NavigationView mNav;

    private HomeFragment homeFragment;
    private FindFragment findFragment;
    private SheQuFragment sheQuFragment;
    private CollectionFragment collectionFragment;

    private int lastCheck;


    @Override
    public int getLayoutId() {
        return R.layout.activity_main;
    }

    @Override
    public void initPresenter() {

    }

    @Override
    public void initView() {
    }

    public static void startAction(Activity activity){
        Intent intent = new Intent(activity, MainActivity.class);
        activity.startActivity(intent);
        activity.overridePendingTransition(R.anim.fade_in,
                R.anim.fade_out);
    }



    @RequiresApi(api = Build.VERSION_CODES.HONEYCOMB)
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        initFragment(savedInstanceState);

        setSupportActionBar(mToolbar);

        ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(this, mDrawer, mToolbar, 0, 0);
        mDrawer.addDrawerListener(toggle);
        toggle.syncState();

        TypedArray typedArray = getTheme().obtainStyledAttributes(new int[]{R.attr.backgroundColor});
        int color = typedArray.getColor(0, 0xffffff);
        mNav.setNavigationItemSelectedListener(this);
        mNav.setBackgroundColor(color);
        //日夜间模式的切换以及navigationview头布局的设置
        initHeadView();

    }

    @RequiresApi(api = Build.VERSION_CODES.HONEYCOMB)
    private void initHeadView() {
        View headerView = mNav.getHeaderView(0);
        ImageView change = (ImageView) headerView.findViewById(R.id.nav_change);
        TextView username = (TextView) headerView.findViewById(R.id.nav_name);
        ImageView userImage = (ImageView) headerView.findViewById(R.id.nav_image);
        if (ThemeChangeUtil.isChange) {
            change.setImageResource(R.drawable.ic_switch_daily);
        } else {
            change.setImageResource(R.drawable.ic_switch_night);
        }

        userImage.setOnClickListener(v-> LoginActivity.startAction(this));
        change.setOnClickListener(v -> {
            if (ThemeChangeUtil.isChange) {
                ThemeChangeUtil.isChange = false;

            } else {
                ThemeChangeUtil.isChange = true;
            }
            MainActivity.this.recreate();
        });

    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
    }

    @Override
    public boolean onNavigationItemSelected(@NonNull MenuItem item) {
        switch (item.getItemId()) {
            case R.id.main_home:
                SwitchTo(0);
                lastCheck = 0;
                break;
            case R.id.main_find:
                SwitchTo(1);
                lastCheck =1;
                break;
            case R.id.main_v:
                SwitchTo(2);
                lastCheck=2;
                break;
            case R.id.main_collect:
                SwitchTo(3);
                lastCheck = 3;
                break;
            case R.id.main_history:
                break;
            case R.id.main_clear:
                ExampleApplication.getMusicDao().deleteAll();
                break;
            case R.id.main_detail:
                break;
        }
        mDrawer.closeDrawer(GravityCompat.START);
        return true;
    }

    /**
     * 初始化碎片
     */
    private void initFragment(Bundle savedInstanceState) {
        FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
        int currentTabPosition = 0;
        if (savedInstanceState != null) {
            homeFragment = (HomeFragment) getSupportFragmentManager().findFragmentByTag("homeFragment");
            findFragment = (FindFragment) getSupportFragmentManager().findFragmentByTag("findFragment");
            sheQuFragment = (SheQuFragment) getSupportFragmentManager().findFragmentByTag("sheQuFragment");
            collectionFragment = (CollectionFragment) getSupportFragmentManager().findFragmentByTag("collectionFragment");
            currentTabPosition = savedInstanceState.getInt(AppConstant.HOME_CURRENT_TAB_POSITION);
        } else {
            homeFragment = new HomeFragment();
            findFragment = new FindFragment();
            sheQuFragment = new SheQuFragment();
            collectionFragment = new CollectionFragment();
            transaction.add(R.id.container, homeFragment, "homeFragment");
            transaction.add(R.id.container, findFragment, "findFragment");
            transaction.add(R.id.container,sheQuFragment,"sheQuFragment");
            transaction.add(R.id.container,collectionFragment,"collectionFragment");
        }
        transaction.commit();
        SwitchTo(currentTabPosition);
        mNav.setCheckedItem(currentTabPosition);
    }
    //跳转fragment
    private void SwitchTo(int currentTabPosition) {
        FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
        switch (currentTabPosition) {
            //漫画城
            case 0:
                transaction.hide(findFragment);
                transaction.hide(sheQuFragment);
                transaction.hide(collectionFragment);
                transaction.show(homeFragment);
                transaction.commitAllowingStateLoss();
                break;
            //发现
            case 1:
                transaction.hide(homeFragment);
                transaction.hide(sheQuFragment);
                transaction.hide(collectionFragment);
                transaction.show(findFragment);
                transaction.commitAllowingStateLoss();
                break;
            //社区
            case 2:
                transaction.hide(homeFragment);
                transaction.hide(findFragment);
                transaction.hide(collectionFragment);
                transaction.show(sheQuFragment);
                transaction.commitAllowingStateLoss();
                break;
            //收藏
            case 3:
                transaction.hide(homeFragment);
                transaction.hide(findFragment);
                transaction.hide(sheQuFragment);
                transaction.show(collectionFragment);
                transaction.commitAllowingStateLoss();
                break;
            default:
                break;
        }
    }
//保存最后一次点击的位置,即使主题切换后还是在你最后一次打开的位置
    @Override
    protected void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
        if (mNav != null) {
            outState.putInt(AppConstant.HOME_CURRENT_TAB_POSITION,lastCheck);
        }
    }

    @Override
    protected void onRestoreInstanceState(Bundle savedInstanceState) {
        super.onRestoreInstanceState(savedInstanceState);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.main_right,menu);
        return super.onCreateOptionsMenu(menu);
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        MusicActivity.startAction(this);
        return super.onOptionsItemSelected(item);
    }
}
